package cn.aitrox.ry.service.anno.dao.impl;

import cn.aitrox.ry.common.bean.Order;
import cn.aitrox.ry.common.dao.MapperSqlCallback;
import cn.aitrox.ry.common.dao.impl.BaseDaoImpl;
import cn.aitrox.ry.service.anno.dao.AnnoResultDao;
import cn.aitrox.ry.service.anno.entity.AnnoResultEntity;
import cn.aitrox.ry.service.anno.mapper.AnnoResultMapper;
import cn.aitrox.ry.service.anno.po.anno.result.AnnoResultQueryParamPo;
import cn.aitrox.ry.service.anno.po.anno.result.SeriesAnnoCountCountPo;
import cn.aitrox.ry.service.anno.po.task.TaskAnnoCountInfoPo;
import cn.aitrox.ry.service.anno.po.task.TaskSeriesCountPo;
import cn.aitrox.ry.util.ArrayUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AnnoResultDaoImpl extends BaseDaoImpl<AnnoResultEntity> implements AnnoResultDao {

    @Autowired
    private AnnoResultMapper mapper;

    @Override
    public List<TaskAnnoCountInfoPo> findAnnoCountInfoByTaskIdsStatus(List<Integer> taskIdList, Integer... status) {
        return mapper.findAnnoCountInfoByTaskIdsStatus(taskIdList, status);
    }

    @Override
    public List<TaskSeriesCountPo> findTaskSeriesCountByTaskIdsUserId(List<Integer> taskIds, Integer userId) {
        return mapper.findTaskSeriesCountByTaskIdsUserId(taskIds, userId);
    }

    @Override
    public List<SeriesAnnoCountCountPo> findSeriesAnnoCountCountByTaskIds(List<Integer> taskIds) {
        return mapper.findSeriesAnnoCountCountByTaskIds(taskIds);
    }

    @Override
    public String findGetableGroupId(Integer taskId, Integer userId, Integer crossNum) {
        return mapper.findGetableGroupId(taskId, userId, crossNum);
    }

    @Override
    public List<AnnoResultEntity> findByTaskIdUserIdGroupId(Integer taskId, Integer userId, String groupId) {
        if (null == taskId || null == userId || null == groupId) {
            return new ArrayList<>();
        }
        AnnoResultEntity cond = new AnnoResultEntity();
        cond.setTaskId(taskId);
        cond.setUserId(userId);
        cond.setGroupId(groupId);
        return super.findByCondition(cond, null);
    }

    @Override
    public List<AnnoResultEntity> findByTaskIdGroupIdUserIdNotGtZero(Integer taskId, String groupId) {
        return mapper.findByTaskIdGroupIdUserIdNotGtZero(taskId, groupId);
    }

    @Override
    public void batchSave(List<AnnoResultEntity> list) {
        if (ArrayUtil.isEmpty(list)) {
            return;
        }
        super.batchInsert(list, 1000, new MapperSqlCallback<AnnoResultEntity>() {
            @Override
            public void batchInsert(List<AnnoResultEntity> objList) {
                mapper.batchInsert(objList);
            }
        });
    }

    @Override
    public Integer findGroupIdCountByTaskIdStatusUserIdGtZero(Integer taskId, Integer... status) {
        return mapper.findGroupIdCountByTaskIdStatusUserIdGtZero(taskId, status);
    }

    @Override
    public List<AnnoResultEntity> findByTaskIdUserIdStatus(Integer taskId, Integer userId, Integer status) {
        if (null == taskId || null == userId) {
            return new ArrayList<>();
        }
        AnnoResultEntity cond = new AnnoResultEntity();
        cond.setTaskId(taskId);
        cond.setUserId(userId);
        cond.setStatus(status);
        return super.findByCondition(cond, null);
    }

    @Override
    public List<Integer> findUserIdsByTaskId(Integer taskId) {
        return mapper.findUserIdsByTaskId(taskId);
    }

    @Override
    public PageInfo<AnnoResultEntity> searchByParmas(Integer page, Integer pageSize, AnnoResultQueryParamPo params) {
        return super.searchByCustom(page, pageSize, new MapperSqlCallback<AnnoResultEntity>() {
            @Override
            public List<AnnoResultEntity> invoke() {
                return mapper.query(params);
            }
        });
    }

    @Override
    public List<AnnoResultEntity> findByTaskIdsStatus(List<Integer> taskIds, Integer... status) {
        return mapper.findByTaskIdsStatus(taskIds, status);
    }

    @Override
    public PageInfo<AnnoResultEntity> searchUserGroupByTaskIdUserIdGroupIdOrderByStatus(Integer page, Integer pageSize, Integer userId, Integer taskId, String groupId) {
        return super.searchResultByCustom(page, pageSize, Order.asc("max(status)"), new MapperSqlCallback<AnnoResultEntity>() {
            @Override
            public List<AnnoResultEntity> invoke() {
                return mapper.findUserGroupByTaskIdUserIdGroupId(taskId, userId, groupId);
            }
        });
    }

    @Override
    public List<AnnoResultEntity> findByTaskIdUserIdGroupIds(Integer taskId, Integer userId, List<String> groupIds) {
        return mapper.findByTaskIdUserIdGroupIds(taskId, userId, groupIds);
    }

    @Override
    public Integer findEnGetableGroupCount(Integer taskId, Integer userId, Integer crossNum) {
        return mapper.findEnGetableGroupCount(taskId, userId, crossNum);
    }
}
